package com.burgeon.backend.SHOPBACKEND.rest;
/**
 * 获取小程序条码
 * 参数 款号 颜色 尺寸
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetGoodsCode extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetGoodsCode(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public String getData(String pcode,String color,String size) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        System.err.println(pcode+","+color+","+size);
        String str = RESULT(Data(pcode,color,size), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception();
        }
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        return jsonArray.getJSONArray(0).getString(0);
    }

    private MultiValueMap Data(String pcode,String color,String size) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("NO");

        params.put("columns", columns);
        params.put("table", "M_PRODUCT_ALIAS");//表名

        params.put("count", "true");

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();
        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();

        expr1.put("combine", "and");
        expr1v1.put("column", "'M_PRODUCT_ID;NAME'");
        expr1v1.put("condition", "'"+pcode+"'");
        expr1.put("expr1", expr1v1);
        expr1v2.put("column", "'M_ATTRIBUTESETINSTANCE_ID;VALUE1_ID;NAME'");
        expr1v2.put("condition", "'"+color+"'");
        expr1.put("expr2", expr1v2);

        expr2.put("column", "'M_ATTRIBUTESETINSTANCE_ID;VALUE2_ID;NAME'");
        expr2.put("condition", "'"+size+"'");

        paramss.put("combine", "and");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetGoodsCode g = new GetGoodsCode("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("A0211TS28","图形色(本白)","4"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
